﻿$(document).ready(function (e) {
    //手机登录验证码 
    function sendyzm(obj) {
        var phone = $("#phone").val();
        $.ajax({
            url: "/Verificationcode/SendCode?tel=" + phone,
            data: { Phone: phone },
            dataType: "json",
            type: "post",
            async: false,
            cache: false,
            success: function (res) {
                //debugger;
                layui.use("layer", function () {
                    if (res.success) {
                        layui.layer.msg("验证码发送成功", { area: '180px'});
                    } else {
                        layui.layer.msg("验证码发送失败", { area: '180px' });
                    }
                });
            },
            error: function () {
                layui.use("layer", function () {
                    layui.layer.msg("验证码发送失败", { area: '180px' });
                });
            }
        })
        setTime(obj);//开始倒计时

    }

    $("#phone").focus(function () {
        $('#beError').hide();
    })

    $("#codebtn").click(function () {
        if ($("input[name=phone]").val() == "") {
            $('#beError').text("请先填写手机号码").show();
        } else {
            sendyzm($("#codebtn"));
        }
    })
    //60s倒计时实现逻辑
    var countdown = 60;
    function setTime(obj) {
        if (countdown == 0) {
            obj.prop('disabled', false);
            obj.val("点击获取验证码");
            obj.css("background-color", "rgb(23, 155, 105)");
            countdown = 60;//60秒过后button上的文字初始化,计时器初始化;
            return;
        } else {
            obj.prop('disabled', true);
            obj.val("(" + countdown + "s)后重新发送");
            obj.css("background-color", "#999");
            countdown--;
        }
        setTimeout(function () { setTime(obj) }, 1000) //每1000毫秒执行一次
    }
    //招人还是找工作
    $('.register_radio li input').click(function (e) {
        $(this).parent('li').addClass('current').append('<em></em>').siblings().removeClass('current').find('em').remove();
        if ($(this).val() == 0) {
            $("#GetJob").show();
            $("#submitLogin").val("注     册");
        } else {
            $("#GetJob").hide();
            $("#submitLogin").val("开通招聘服务");
        }
    });

    $(".nextRegister").click(function () {
        if ($(".register_radio li input:checked").val() != 0) {

            location.href = "/Company/Bindstep1";
        }
    })
    ///验证验证码是否正确
    function CoreIsTrue() {
        var flag = false;
        if ($("#code").val().length != 6) {//不是6位不判断,提高效率
            return false;
        }
        var data = {};
        data.Phone = $("#phone").val();
        data.CodeNum = $("#code").val();
        $.ajax({
            async: false,
            type: "post",
            url: "/Verificationcode/ValidateCode",
            data: data,
            success: function (result) {
                if (result.success) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
        })
        return flag;
    }
    //自定义表单验证
    jQuery.validator.addMethod("CoreIsTrue", function (value, element) {
        var length = value.length;
        var isTrue = CoreIsTrue();
        return this.optional(element) || isTrue;
    }, "请输入正确的验证码");



    //验证表单
    $("#loginForm").validate({
        rules: {
            type: {
                required: true
            },
            email: {
                required: true,
                email: true
            },
            username: {
                required: true
            },
            password: {
                required: true,
                rangelength: [6, 16]
            },
            checkbox: { required: true },
            phone: {
                required: true,
                minlength: 11,
                maxlength: 11,
                isphoneNum: true
            },
            code: {
                required: true,
                //判断验证码是否正确
                range: [100000, 999999],//输入的验证码范围            
                CoreIsTrue: true
            },
        },
        messages: {
            type: {
                required: "请选择使用1024的目的"
            },
            email: {
                required: "请输入常用邮箱地址",
                email: "请输入有效的邮箱地址，如：vivi@lagou.com"
            },
            username: {
                required: "请输入用户名"
            },
            password: {
                required: "请输入密码",
                rangelength: "请输入6-16位密码，字母区分大小写"
            },
            checkbox: {
                required: "请接受1024用户协议"
            }, phone: {
                required: "请输入手机号码",
                minlength: "请输入11位手机号码",
                maxlength: "请输入11位手机号码",
                isphoneNum: "请正确填写手机号码"
            },
            code: {
                required: "请输入验证码",
                range: "请输入6位验证码",
                CoreIsTrue: "请输入正确的验证码"
            }
        },
        errorPlacement: function (label, element) {/*
                                                                                /*modify nancy*/
            if (element.attr("type") == "radio") {
                label.insertAfter($(element).parents('ul')).css('marginTop', '-20px');
            } else if (element.attr("type") == "checkbox" || element.attr("name") == "code") {
                label.insertAfter($(element).parent()).css('clear', 'left');
                //} else if (element.attr("name")=="code") {
                //    label.insertAfter($(element).parent());
            } else {
                label.insertAfter(element);
            };
        },
        submitHandler: function (form) {
            //$(form).find(":submit").attr("disabled", true);
            var data = {};
            data.Email = $('#email').val();
            data.UserPwd = $('#password').val();
            data.UserName = $('#username').val();
            //data.UserType = $('input[type="radio"]:checked', form).val();
            //layui.alert(data.UserType);
            data.Phone = $("#phone").val();
            $.ajax({
                type: 'post',
                url: "/Login/AddUserinfo",
                data: data,
                success: function (result) {
                    if (result.success) {
                        layui.use("layer", function () {
                            layui.layer.msg("注册成功", { area: '120px' });
                        });
                        location.href = "/Login/Login";
                    } else {
                        layui.layer.msg("注册失败", { area: '120px' });
                        $('#beError').text(result.msg).show();
                    }
                }
            }).done(function (result) {
                $(form).find(":submit").attr("disabled", false);
            });
        }
    })
    //自定义手机号验证
    jQuery.validator.addMethod("isphoneNum", function (value, element) {
        //debugger
        var length = value.length;
        var mobile = /^1[3|5|8]|[7]|[9]{1}[0-9]{9}$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写您的手机号码");
})